﻿using System;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.UserSkins;
using Moonlite.Data.Configuration.Implementation;
using Moonlite.Platform.General;

namespace Moonlite.Data.Configuration.UI
{
    public partial class ConfigurationForm : Form
    {
        private void InitSkinGallery()
        {
            selectTheme.Items.Clear();
            selectTheme.Text = Global.Instance.ActiveSkinName;
            foreach (string skin in Global.Instance.GetAllSkinNames())
            {
                selectTheme.Items.Add(skin);
            }
        }

        private void OnSelectThemeComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            string skinName = ((ComboBox)sender).Text;
            Global.Instance.SetSkin(skinName);
        }

        private void InitDefaultValues()
        {
            ConfigurationManager.LoadAssemblyConfigurationAttachment(Moonlite.Shell.Attachment);
            MoonliteGeneralConfigurationElement generalElement = (MoonliteGeneralConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("General");
            MoonliteBuildConfigurationElement buildElement = (MoonliteBuildConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("Build");
            MoonliteTextEditorConfigurationElement textEditorElement = (MoonliteTextEditorConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("Text Editor");
            showSplashScreen.Checked = generalElement.ShowSplashScreen;
            selectTheme.SelectedItem = generalElement.Theme;
            fontName.Text = textEditorElement.FontName;
            fontSize.Value = textEditorElement.FontSize;
            allowDynamicParsing.Checked = textEditorElement.AllowDynamicParsing;
            dynamicParseInterval.Value = textEditorElement.DynamicParseInterval;
            convertTabsToSpaces.Checked = textEditorElement.ConvertTabsToSpaces;
            saveProjectsBeforeBuild.Checked = buildElement.SaveProjectBeforeCompiling;
        }

        private void Save()
        {
            Moonlite.Shell.Attachment.AutoSave = false;
            MoonliteGeneralConfigurationElement generalElement = (MoonliteGeneralConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("General");
            MoonliteBuildConfigurationElement buildElement = (MoonliteBuildConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("Build");
            MoonliteTextEditorConfigurationElement textEditorElement = (MoonliteTextEditorConfigurationElement)Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("Text Editor");
            generalElement.ShowSplashScreen = showSplashScreen.Checked;
            generalElement.Theme = (string)selectTheme.SelectedItem;
            textEditorElement.FontName = fontName.Text;
            textEditorElement.FontSize = (int)fontSize.Value;
            textEditorElement.AllowDynamicParsing = allowDynamicParsing.Checked;
            textEditorElement.DynamicParseInterval = (int)dynamicParseInterval.Value;
            textEditorElement.ConvertTabsToSpaces = convertTabsToSpaces.Checked;
            buildElement.SaveProjectBeforeCompiling = saveProjectsBeforeBuild.Checked;
            ConfigurationManager.SaveConfigurationInstanceToConfigurationDocument(Moonlite.Shell.Attachment.ConfigurationInstance, Moonlite.Shell.Attachment.ConfigurationStoragePath, true);
            Moonlite.Shell.Attachment.AutoSave = true;
        }

        /// <inheritdoc />
        public new void Show()
        {
            InitSkinGallery();
            InitDefaultValues();
            base.Show();
        }

        /// <inheritdoc />
        public new void ShowDialog()
        {
            InitSkinGallery();
            InitDefaultValues();
            base.ShowDialog();
        }

        /// <inheritdoc />
        public new void ShowDialog(IWin32Window owner)
        {
            InitSkinGallery();
            InitDefaultValues();
            base.ShowDialog(owner);
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            InitDefaultValues();
            Close();
        }

        private void OkButtonClick(object sender, EventArgs e)
        {
            Save();
            Close();
        }

        public ConfigurationForm()
        {
            InitializeComponent();
        }
    }
}
